package com.qf.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by 54110 on 2021/5/12.
 */
@Controller
public class TestPayController {

    @RequestMapping("/pay")
    public void pay(HttpServletRequest httpRequest,
                    HttpServletResponse httpResponse) throws IOException, AlipayApiException {
        //构造支付宝证书的client
        CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
        //设置请求的支付宝路径
        certAlipayRequest.setServerUrl("https://openapi.alipaydev.com/gateway.do");
        //设置当前的支付宝appid
        certAlipayRequest.setAppId("2016110100784985");
        //设置应用私钥 应用私钥在支付宝开放平台助手生成crt时自动生成
        certAlipayRequest.setPrivateKey("MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCsDDx/1iLwsN05YVp7HGYqvV/LP66kY383HzQqR71Z7bcAVKbQytUpc3jZxQp3IbM0mizF/2JEwUHbefPeHR4JkkPc/Nc2ZDRek2K0IT77mm3BhVOMQ7yd2msvokfkrEL2AByAkpGOr05jyCRR/g9HqltAtsrBQ8sPI9iEHcBKH6lc4c9dgASCVQFmg3gUwvtNcyGqIOPoR8uTB6HYrGXHjRALCPSiMjVnlkbBP5VzEV2puqAHkHrxy82L4tKEl3polmsQFBy5Ni8RBNj8gYAGli/CFb617JG+3xcfIcL351lekKf2xuUA75bI+MAdJSAP/fda6Mzl5L+5EPU0fJSZAgMBAAECggEAWWKuF/ioBIbPGN20hZu2lOP4nf9C+9NZp8jFcGX/QXaYikpwtK982eFNW07c5hITKfQj6OaLJo+FaMLOaYqVLYHZUjnmkjKzZqOKN23CbDPKbC2Zv5j9qMFuyLu2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RydnE6NhpSubsjWvaC2yMBCB2D3CU\n");
        //设置格式化方式
        certAlipayRequest.setFormat("json");
        //设置编码格式
        certAlipayRequest.setCharset("utf-8");
        //设置加密的方式
        certAlipayRequest.setSignType("RSA2");
        //设置应用公钥的绝对路径-->下载的公钥证书
        certAlipayRequest.setCertPath("C:\\Users\\54110\\Desktop\\appCertPublicKey_2016110100784985.crt");
        //设置支付宝公钥的绝对路径-->下载的公钥证书
        certAlipayRequest.setAlipayPublicCertPath("C:\\Users\\54110\\Desktop\\alipayCertPublicKey_RSA2.crt");
        //设置支付宝根的绝对路径-->下载的公钥证书
        certAlipayRequest.setRootCertPath("C:\\Users\\54110\\Desktop\\alipayRootCert.crt");
        //创建默认的支付宝客户端
        DefaultAlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest);
        //创建电脑网站支付的请求客户端
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        //设置支付宝成功后的同步回调
        alipayRequest.setReturnUrl( "http://domain.com/CallBack/return_url.jsp" );
        //设置支付宝成功后的异步回调
        alipayRequest.setNotifyUrl( "http://domain.com/CallBack/notify_url.jsp" );
        // new AlipayTradeAppPayRequest() 这个模型是app的请求方式
        //声明电脑网站支付请求
        //AlipayTradePagePayRequest model = new AlipayTradePagePayRequest();
        // AlipayTradeAppPayModel 这个模型是app的请求模型
        //AlipayTradePagePayModel 这个模式时web请求模型
        AlipayTradePagePayModel model = new AlipayTradePagePayModel();
        model.setBody("我是测试数据");
        model.setSubject("App支付测试Java");
        //out_trade_no 订单号，客户自定义
        model.setOutTradeNo("123321321");
        //超时时间
        model.setTimeoutExpress("30m");
        //价格
        model.setTotalAmount("0.01");
        //app支付时productCode参数的值为 "QUICK_MSECURITY_PAY"；
        //web支付时为 FAST_INSTANT_TRADE_PAY
        model.setProductCode("FAST_INSTANT_TRADE_PAY");

        alipayRequest.setBizModel(model); //填充业务参数




        String form="";
        try  {
            //发起请求
            AlipayTradePagePayResponse alipayTradePagePayResponse = alipayClient.pageExecute(alipayRequest);
            //获取返回参数
            form=alipayTradePagePayResponse.getBody();
        }  catch  (AlipayApiException e) {
            e.printStackTrace();
        }
        httpResponse.setContentType( "text/html;charset=utf-8");
        httpResponse.getWriter().write(form); //直接将完整的表单html输出到页面
        httpResponse.getWriter().flush();
        httpResponse.getWriter().close();
    }
}
